// pages/consult_detail/person/person.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    consultInfo: {},
    List:{},
    // 回答的和问的
    ask:[],
    // 输入的内容
    content:null,
    num:2,
    consultationappointmentInfo:{},
    consultationappointmentList:{},
    privatemessage:{},
    starArrays: {},
    courselist:{},

  },
  enterBack(){
    wx.switchTab({
      url: '../../consult/consult',
    })
  },
  enterMessage:function(){
    wx.navigateTo({
      url: '../../message/message',
    })
  },
  entercertificate:function(e){
    const consultantId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../consultantcertificate/consultantcertificate?id=${consultantId}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const consultantId = options.id;
    this.fetchConsultantDetail(consultantId);
    this.fetchConsultantAppointmentDetail(consultantId);
    this.fetchPrivatemessageDetail(consultantId);
    this.fetchCourseDetail(consultantId);
  },
  changeStatus:function(){

  },
  fetchConsultantDetail: function(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8085/consultant/findConsultantById?id=${id}`,
      success: function(res) {
        console.log(res.data.data);
        if (res.data.data) {
          that.setData({
            consultInfo: res.data.data,
            List:res.data.data.chargeassociationList,
          });
        } else {
          wx.showToast({
            title: '获取详情失败',
            icon: 'none'
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },
  fetchConsultantAppointmentDetail(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8085/member/consultationappointment?id=${id}`,
      success(res) {
        // console.log(res.data.data);
        if (res.data.data) {
          that.setData({
            consultationappointmentInfo: res.data.data,
          }, () => {
            // 在成功设置咨询预约信息后，生成星星数组
            that.generateStarArrays();
          });
        } else {
          wx.showToast({ title: '获取详情失败', icon: 'none' });
        }
      },
      fail() {
        wx.showToast({ title: '请求失败', icon: 'none' });
      }
    });
  },
  fetchPrivatemessageDetail: function(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8085/privatemessage/selectMessage?consultantId=${id}`,
      success: function(res) {
        // console.log(res.data.data);
        if (res.data.data) {
          that.setData({
            privatemessage: res.data.data,
          });
        } else {
          wx.showToast({
            title: '获取详情失败',
            icon: 'none'
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },
  fetchCourseDetail: function(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8085/course/id?consultantId=${id}`,
      success: function(res) {
        // console.log(res.data.data);
        if (res.data.data) {
          that.setData({
            courselist: res.data.data,
          });
        } else {
          wx.showToast({
            title: '获取详情失败',
            icon: 'none'
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },
  enterCourse(event){
    const title = event.currentTarget.dataset.title
    const id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: `../../course_detail/course_in/course_in?courseId=${id}&title=${title}`,
    })
  },
  enterZiXun(e){
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../zixun/zixun?id=${id}`,
    })
  },
  // 新增：生成星星数组的方法
  generateStarArrays() {
    const starArrays = {};
    const { consultationappointmentInfo } = this.data;

    if (Array.isArray(consultationappointmentInfo)) {
      consultationappointmentInfo.forEach(item => {
        if (item.consultationappointmentList && item.consultationappointmentList.length > 0) {
          item.consultationappointmentList.forEach(appointment => {
            if (appointment.consultationreview) {
              const stars = appointment.consultationreview.stars || 0;
              starArrays[appointment.id] = this.getStarArray(stars);
            }
          });
        }
      });
    }

    this.setData({ starArrays });
    // console.log(starArrays);
  },

  // 新增：获取星星数组的方法
  getStarArray(stars) {
    return Array.from({ length: 5 }, (_, i) => ({
      isFull: i < stars
    }));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(wx.getStorageSync('memberId')){
    let that = this
    wx.request({
      url: 'http://localhost:8085/privatemessage/selectMessage',
      method:'GET',
      data:{
        memberId:wx.getStorageSync('memberId'),
        // 先这样写着
        consultantId:1
      },
      success(res){
        that.setData({
          ask:res.data.data
        })
      },
      fail(err){
        console.log(err);
      }
    });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})